import { Switch } from 'ant-design-vue'
import { h } from 'vue'
import type { BasicColumn, FormSchema } from '@/components/Table'
import { useRender } from '@/components/Table'
import { DICT_TYPE, getDictOptions } from '@/utils/dict'
import { useUserStore } from '@/store/modules/user'
import { ycListSimple } from '@/api/wages/salaryslip'
import { useMessage } from '@/hooks/web/useMessage'
import { updateRecordConfigEnable } from '@/api/wages/recordconfig'

const userStore = useUserStore()
const ycListSimpleRes = await ycListSimple()
export const columns: BasicColumn[] = [
  {
    title: '类型',
    dataIndex: 'type',
    width: 180,
    customRender: ({ text }) => {
      return useRender.renderDict(text, DICT_TYPE.RECORD_CONFIG)
    },
  },
  {
    title: '按次分类',
    dataIndex: 'secondType',
    width: 180,
    customRender: ({ text }) => {
      if (!text)
        return '无'
      return useRender.renderDict(text, DICT_TYPE.RECORD_CONFIG_ONCE)
    },
  },
  {
    title: '名称',
    dataIndex: 'name',
    width: 160,
  },
  {
    title: '单价',
    dataIndex: 'unitPrice',
    width: 160,
  },
  {
    title: '所属部门',
    dataIndex: 'deptName',
    width: 180,
    ifShow: () => userStore.getUserInfo.roles.some(item => item === 'city_admin' || item === 'super_admin' || item === 'wages_admin'),
  },
  {
    title: '是否启用',
    dataIndex: 'enable',
    width: 180,
    customRender: ({ record }) => {
      if (!Reflect.has(record, 'pendingStatus'))
        record.pendingStatus = false

      return h(Switch, {
        checked: record.enable,
        checkedChildren: '已启用',
        unCheckedChildren: '已禁用',
        loading: record.pendingStatus,
        onChange(checked: boolean) {
          record.pendingStatus = true
          const { createMessage } = useMessage()
          updateRecordConfigEnable(record.id, checked)
            .then(() => {
              record.enable = checked
              createMessage.success('已成功修改配置状态')
            })
            .catch(() => {
              createMessage.error('修改配置状态失败')
            })
            .finally(() => {
              record.pendingStatus = false
            })
        },
      })
    },
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    width: 180,
    customRender: ({ text }) => {
      return useRender.renderDate(text)
    },
  },
]

export const searchFormSchema: FormSchema[] = [
  {
    label: '类型',
    field: 'type',
    component: 'Select',
    componentProps: {
      options: getDictOptions(DICT_TYPE.RECORD_CONFIG, 'string'),
    },
    colProps: { span: 8 },
  },
  {
    label: '按次分类',
    field: 'secondType',
    component: 'Select',
    ifShow: ({ values }) => values.type === 'once',
    componentProps: {
      options: getDictOptions(DICT_TYPE.RECORD_CONFIG_ONCE, 'string'),
    },
    colProps: { span: 8 },
  },
  {
    label: '名称',
    field: 'name',
    component: 'Input',
    colProps: { span: 8 },
  },
  {
    label: '单价',
    ifShow: () => false,
    field: 'unitPrice',
    component: 'Input',
    colProps: { span: 8 },
  },
  {
    label: '所属部门',
    field: 'deptId',
    component: 'Select',
    ifShow: () => userStore.getUserInfo.roles.some(item => item === 'city_admin' || item === 'super_admin' || item === 'wages_admin'),
    componentProps: {
      options: ycListSimpleRes,
      fieldNames: {
        label: 'name',
        value: 'id',
      },
    },
    colProps: { span: 8 },
  },
  {
    label: '创建时间',
    field: 'createTime',
    component: 'RangePicker',
    colProps: { span: 8 },
  },
]

export const createFormSchema: FormSchema[] = [
  {
    label: '编号',
    field: 'id',
    show: false,
    component: 'Input',
  },
  {
    label: '类型',
    field: 'type',
    required: true,
    component: 'Select',
    componentProps: {
      options: getDictOptions(DICT_TYPE.RECORD_CONFIG, 'string'),
    },
  },
  {
    label: '按次分类',
    field: 'secondType',
    component: 'Select',
    ifShow: ({ values }) => values.type === 'once',
    componentProps: {
      options: getDictOptions(DICT_TYPE.RECORD_CONFIG_ONCE, 'string'),
    },
  },
  {
    label: '名称',
    field: 'name',
    required: true,
    component: 'Input',
  },
  {
    label: '描述',
    field: 'describe',
    required: true,
    component: 'Input',
  },
  {
    label: '单价',
    field: 'unitPrice',
    required: true,
    component: 'InputNumber',
    componentProps: {
      prefix: '￥',
      style: 'width: 100%',
      stringMode: true,
      precision: 2,
      min: 0,
      max: 999999,
    },
  },
  {
    label: '所属部门',
    field: 'deptId',
    required: true,
    component: 'Select',
    ifShow: () => userStore.getUserInfo.roles.some(item => item === 'city_admin' || item === 'super_admin' || item === 'wages_admin'),
    componentProps: {
      options: ycListSimpleRes,
      fieldNames: {
        label: 'name',
        value: 'id',
      },
    },
  },
]

export const updateFormSchema: FormSchema[] = [
  {
    label: '编号',
    field: 'id',
    show: false,
    component: 'Input',
  },
  {
    label: '类型',
    field: 'type',
    required: true,
    component: 'Select',
    dynamicDisabled: true,
    componentProps: {
      options: getDictOptions(DICT_TYPE.RECORD_CONFIG, 'string'),
    },
  },
  {
    label: '按次分类',
    field: 'secondType',
    component: 'Select',
    ifShow: ({ values }) => values.type === 'once',
    dynamicDisabled: true,
    componentProps: {
      options: getDictOptions(DICT_TYPE.RECORD_CONFIG_ONCE, 'string'),
    },
  },
  {
    label: '名称',
    field: 'name',
    required: true,
    dynamicDisabled: true,
    component: 'Input',
  },
  {
    label: '描述',
    field: 'describe',
    required: true,
    component: 'Input',
  },
  {
    label: '单价',
    field: 'unitPrice',
    required: true,
    component: 'Input',
  },
  {
    label: '所属部门',
    field: 'deptId',
    required: true,
    component: 'Select',
    dynamicDisabled: true,
    ifShow: () => userStore.getUserInfo.roles.some(item => item === 'city_admin' || item === 'super_admin' || item === 'wages_admin'),
    componentProps: {
      options: ycListSimpleRes,
      fieldNames: {
        label: 'name',
        value: 'id',
      },
    },
  },
]
